package com.aplink.dellcommercial.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitmapUtis {
    public static int calculateInSampleSize( BitmapFactory.Options options,
                                             int reqWidth, int reqHeight ) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if ( ( height > reqHeight ) || ( width > reqWidth ) ) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while ( ( ( halfHeight / inSampleSize ) > reqHeight )
                    && ( ( halfWidth / inSampleSize ) > reqWidth ) ) {
                inSampleSize *= 2;
            }

            long totalPixels = ( width * height ) / inSampleSize;

            final long totalReqPixelsCap = reqWidth * reqHeight * 2;

            while ( totalPixels > totalReqPixelsCap ) {
                inSampleSize *= 2;
                totalPixels /= 2;
            }
        }
        return inSampleSize;
    }

    ;

    static BitmapFactory.Options getDefaultOptions() {
        BitmapFactory.Options options = new BitmapFactory.Options();

        return options;
    }

    public static Bitmap decodeSampledBitmapFromFile( String filename,
                                                      int reqWidth, int reqHeight ) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile( filename, options );
        options.inSampleSize = calculateInSampleSize( options, reqWidth,
                reqHeight );
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inDither = false;
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];
        return BitmapFactory.decodeFile( filename, options );
    }

    public static Bitmap decodeSampledBitmapFromResource( Resources res,
                                                          int resId, int reqWidth, int reqHeight ) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource( res, resId, options );

        options.inSampleSize = calculateInSampleSize( options, reqWidth,
                reqHeight );

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource( res, resId, options );
    }

    public static Bitmap decodeThumbnailBitmapFromFile( String filename ) {
        return decodeSampledBitmapFromFile( filename, 640, 480 );
    }

    public static Bitmap decodeThumbnailBitmapFromResource( Resources res,
                                                            int resId ) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource( res, resId, options );

        options.inSampleSize = calculateInSampleSize( options, 150, 150 );

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource( res, resId, options );
    }

    private BitmapUtis() {
    }
}
